var xlsx = require('node-xlsx').default;
const fs = require('fs');
var buffer = [
    {
        name: '含量下降超过50%',
        data: []
    },
    {
        name: '直接消失的物质',
        data: []
    }
];
let deepClone = require("./deepcopy")
function removeEmptyArrayEle(arr){
    for(var i = 0; i < arr.length; i++) {
        if(arr[i] == undefined) {
            arr.splice(i,1);
            i = i - 1; // i - 1 ,因为空元素在数组下标 2 位置，删除空之后，后面的元素要向前补位，
            // 这样才能真正去掉空元素,觉得这句可以删掉的连续为空试试，然后思考其中逻辑
        }
    }
    return arr;
}
function removeArea0(arr){
    for(var i = 0; i < arr.length; i++) {
        if(arr[i][20] == 0) {
            arr.splice(i,1);
            i = i - 1; // i - 1 ,因为空元素在数组下标 2 位置，删除空之后，后面的元素要向前补位，
            // 这样才能真正去掉空元素,觉得这句可以删掉的连续为空试试，然后思考其中逻辑
        }
        if(Math.abs(arr[i][36])  >= 15) {
            arr.splice(i,1);
            i = i - 1; // i - 1 ,因为空元素在数组下标 2 位置，删除空之后，后面的元素要向前补位，
            // 这样才能真正去掉空元素,觉得这句可以删掉的连续为空试试，然后思考其中逻辑
        }
    }
    return arr;
}
var excelNameList = [];
//判断几个excel，两个的话取出里面的值作比较
var readDir = fs.readdirSync("./import");
for (let index of readDir) {
    excelNameList.push(index)
}
if ( excelNameList.length !== 2){
    console.log("只能同时比较两个文件")
}else{
    var firstExcel = deepClone(xlsx.parse(`./import/${excelNameList[0]}`)[0].data);
    var secondExcel = deepClone(xlsx.parse(`./import/${excelNameList[1]}`)[0].data);
    firstExcel = removeArea0(firstExcel)
    secondExcel = removeArea0(secondExcel)
}
var firstExcel_namelist = [];
var secondExcel_namelist = [];
var public1 = [];
var public2 = [];
var dispear = [];
//
// console.log(firstExcel,secondExcel)
for (let itemArray of firstExcel){
    firstExcel_namelist.push(itemArray[1])
    // console.log(itemArray[36])
}
for (let itemArray of secondExcel){
    // console.log(itemArray[36])
    secondExcel_namelist.push(itemArray[1])
}

for (let i=0;i<firstExcel_namelist.length;i++){
    if(secondExcel_namelist.indexOf(firstExcel_namelist[i]) !==-1){
        public1.push(firstExcel[i])
        // console.log(firstExcel[i][1])
    }
    if(secondExcel_namelist.indexOf(firstExcel_namelist[i]) ===-1){
        dispear.push(firstExcel[i])
        // console.log(firstExcel[i][1])
    }
}
for (let i=0;i<secondExcel_namelist.length;i++){
    if(firstExcel_namelist.indexOf(secondExcel_namelist[i]) !==-1){
        public2.push(secondExcel[i])
        // console.log(secondExcel[i][1])
    }
}
// public1.sort(function(a, b){return a[1] - b[1]});
// public2.sort(function(a, b){return a[1] - b[1]});
console.log(public1.length)
console.log(public2.length)
var public2_sort =[]
for (let i=0;i<public1.length;i++){
    for (let j=0;j<public2.length;j++){
        if(public2[j][1] === public1[i][1]){
            public2_sort[i] = public2[j]
        }
    }
}
public2 =deepClone(removeEmptyArrayEle(public2_sort))

// console.log(public1)
// console.log(public2)
// console.log(public1[1][1])
// console.log(public2[1][1])
// console.log(public1[7][1])
// console.log(public2[7][1])
// console.log(public1[11][1])
// console.log(public2[11][1])
// console.log(public1[19][1])
// console.log(public2[19][1])
// console.log(public1[29][1])
// console.log(public2[29][1])
// console.log(public1[39][1])
// console.log(public2[39][1])

var down50 = [];
for(let i=0;i<public1.length;i++){
    if(Math.abs(public1[i][20]) < Math.abs(public2[i][20]*0.5)){
        var percent = Math.abs(public2[i][20])/Math.abs(public1[i][20]) *100
        public1[i][43] = `${percent} %`
       down50.push(public1[i])
    }
}
var titleArray = deepClone(firstExcel[0])
titleArray[43] = "百分比(未失活area值/失活后的area值)"
down50.unshift(titleArray)

console.log('操作成功');
console.log(`改变超过50%的样本共${down50.length}个，已经写入output文件夹下的excel中`)

buffer[0].data.push(...down50)
buffer[1].data.push(...dispear)
// console.log( buffer[0].data)
buffer = xlsx.build(buffer)
fs.writeFileSync('./output/result(最好重命名后经行下一步操作).xlsx', buffer, {'flag':'w'}); // 如果文件存在，覆盖
